package org.study.redis.api.util.lettuce;

import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.support.ConnectionPoolSupport;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.study.redis.api.constant.RedisProperties;

import java.time.Duration;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2022-12-02
 */
public class SysLettucePool {
    private static GenericObjectPool<StatefulRedisConnection<String, String>> pool = null;

    static {
        // 创建 RedisClient：方式一
        RedisURI redisURI01 = RedisURI.create(RedisProperties.HOST, RedisProperties.PORT);
        // redisURI01.setPassword(RedisProperties.PASSWORD);
        redisURI01.setCredentialsProvider(new StaticCredentialsProvider(null, RedisProperties.PASSWORD.toCharArray()));
        redisURI01.setTimeout(Duration.ofSeconds(RedisProperties.TIMEOUT));
        redisURI01.setDatabase(RedisProperties.DATABASE);
        RedisClient client01 = RedisClient.create(redisURI01);

        // 创建 RedisClient：方式二
        /* StringBuilder uriBuilder02 = new StringBuilder();
        uriBuilder02.append("redis://")
                .append(RedisProperties.PASSWORD)
                .append("@")
                .append(RedisProperties.HOST)
                .append(":")
                .append(RedisProperties.PORT)
                .append("/")
                .append(RedisProperties.DATABASE);
        RedisURI redisURI02 = RedisURI.create(uriBuilder02.toString());
        redisURI02.setTimeout(Duration.ofSeconds(RedisProperties.TIMEOUT));
        RedisClient client02 = RedisClient.create(redisURI02); */

        // 创建 RedisClient：方式三
        /* RedisURI redisURI03 = RedisURI.Builder.redis(RedisProperties.HOST)
                .withPort(RedisProperties.PORT)
                .withPassword(RedisProperties.PASSWORD)
                .withDatabase(0)
                .withTimeout(Duration.ofSeconds(RedisProperties.TIMEOUT))
                .build();
        RedisClient client03 = RedisClient.create(redisURI03); */

        // 创建 RedisClient：方式四
        /* StringBuilder uriBuilder04 = new StringBuilder();
        uriBuilder04.append("redis://")
                .append(RedisProperties.PASSWORD)
                .append("@")
                .append(RedisProperties.HOST)
                .append(":")
                .append(RedisProperties.PORT)
                .append("/")
                .append(RedisProperties.DATABASE);
        RedisClient client04 = RedisClient.create(uriBuilder04.toString()); */

        pool = ConnectionPoolSupport.createGenericObjectPool(() -> client01.connect(), new GenericObjectPoolConfig());
    }

    private SysLettucePool() {
        throw new AssertionError("不能实例化工具类");
    }

    public static StatefulRedisConnection<String, String> borrowObject() throws Exception {
        return pool.borrowObject();
    }

    public static void close(GenericObjectPool pool) {
        if (pool != null) {
            pool.close();
        }
    }
}
